<template>
  <div class="monthStatementBox">
    <!-- 头部标题 -->
    <div class="monthStatementTop">
      <div class="title">社保列表</div>
    </div>
    <!-- 放置列表 -->
    <div class="monthStatementTable">
      <div>
        <div class="topLab">
          <div><span style="background-color:#cfeffe;" />已离职</div>
          <div><span style="background-color:#a8f8bb;" />再入职</div>
          <div><span style="background-color:#fedbd7;" />公司合计</div>
          <div><span style="background-color:#ffe8c9;" />一级部门</div>
          <div><span style="background-color:#fdfcd5;" />二级部门</div>
          <div>
            <el-button type="primary" size="small" class="rightLabBox" @click="exportMonthState">导出</el-button>
          </div>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="monthStatementData"
        border
        style="width: 100%"
        empty-text="暂无数据"
      >
        <el-table-column type="index" label="序号" center width="50" />
        <el-table-column prop="username" label="姓名" width="150px" />
        <el-table-column prop="timeOfEntry" label="入职时间" width="150px">
          <template slot-scope="scope">
            {{ scope.row.timeOfEntry | formatDate }}
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" width="150px" />
        <el-table-column prop="idNumber" label="身份证号码" width="150px" />
        <el-table-column prop="theHighestDegreeOfEducation" label="学历" width="150px" />
        <el-table-column prop="bankCardNumber" label="开户行" width="150px" />
        <el-table-column prop="firstLevelDepartment" label="一级部门" width="150px" />
        <el-table-column prop="twoLevelDepartment" label="二级部门" width="150px" />
        <el-table-column prop="workingCity" label="工作城市" width="150px" />
        <el-table-column prop="socialSecurityComputerNumber" label="社保电脑号" width="150px" />
        <el-table-column prop="providentFundAccount" label="公积金账号" width="150px" />
        <el-table-column prop="leaveDate" label="离职时间" width="150px" />
        <el-table-column prop="householdRegistrationType" label="户籍类型" width="150px" />
        <el-table-column prop="participatingInTheCity" label="参保城市" width="150px" />
        <el-table-column prop="socialSecurityMonth" label="社保月份" width="150px" />
        <el-table-column prop="socialSecurityBase" label="社保基数" width="150px" />
        <el-table-column prop="socialSecurity" label="社保合计" width="150px" />
        <el-table-column prop="socialSecurityEnterprise" label="社保企业" width="150px" />
        <el-table-column prop="socialSecurityIndividual" label="社保个人" width="150px" />
        <el-table-column prop="providentFundCity" label="公积金城市" width="150px" />
        <el-table-column prop="providentFundMonth" label="公积金月份" width="150px" />
        <el-table-column prop="providentFundBase" label="公积金基数" width="150px" />
        <el-table-column prop="accumulationFundEnterpriseBase" label="公积金企业基数" width="150px" />
        <el-table-column
          prop="proportionOfProvidentFundEnterprises"
          label="公积金企业比例"
          width="150px"
        />
        <el-table-column prop="individualBaseOfProvidentFund" label="公积金个人基数" width="150px" />
        <el-table-column prop="personalRatioOfProvidentFund" label="公积金个人比例" width="150px" />
        <el-table-column prop="totalProvidentFund" label="公积金合计" width="150px" />
        <el-table-column prop="providentFundEnterprises" label="公积金企业" width="150px" />
        <el-table-column prop="providentFundIndividual" label="公积金个人" width="150px" />
        <el-table-column prop="pensionEnterpriseBase" label="养老企业基数" width="150px" />
        <el-table-column prop="proportionOfPensionEnterprises" label="养老企业比例" width="150px" />
        <el-table-column prop="pensionEnterprise" label="养老企业" width="150px" />
        <el-table-column prop="personalPensionBase" label="养老个人基数" width="150px" />
        <el-table-column prop="personalPensionRatio" label="养老个人比例" width="150px" />
        <el-table-column prop="oldAgeIndividual" label="养老个人" width="150px" />
        <el-table-column prop="unemploymentEnterpriseBase" label="失业企业基数" width="150px" />
        <el-table-column prop="proportionOfUnemployedEnterprises" label="失业企业比例" width="150px" />
        <el-table-column prop="unemployedEnterprise" label="失业企业" width="150px" />
        <el-table-column prop="theNumberOfUnemployedIndividuals" label="失业个人基数" width="150px" />
        <el-table-column prop="percentageOfUnemployedIndividuals" label="失业个人比例" width="150px" />
        <el-table-column prop="unemployedIndividual" label="失业个人" width="150px" />
        <el-table-column prop="medicalEnterpriseBase" label="医疗企业基数" width="150px" />
        <el-table-column prop="proportionOfMedicalEnterprises" label="医疗企业比例" width="150px" />
        <el-table-column prop="medicalEnterprise" label="医疗企业" width="150px" />
        <el-table-column prop="medicalPersonalBase" label="医疗个人基数" width="150px" />
        <el-table-column prop="medicalPersonalRatio" label="医疗个人比例" width="150px" />
        <el-table-column prop="medicalIndividual" label="医疗个人" width="150px" />
        <el-table-column prop="baseOfIndustrialInjuryEnterprises" label="工伤企业基数" width="150px" />
        <el-table-column
          prop="proportionOfIndustrialInjuryEnterprises"
          label="工伤企业比例"
          width="150px"
        />
        <el-table-column prop="industrialInjuryEnterprise" label="工伤企业" width="150px" />
        <el-table-column prop="fertilityEnterpriseBase" label="生育企业基数" width="150px" />
        <el-table-column prop="proportionOfFertilityEnterprises" label="生育企业比例" width="150px" />
        <el-table-column prop="childbearingEnterprise" label="生育企业" width="150px" />
        <el-table-column prop="baseOfSeriousIllness" label="大病企业基数" width="150px" />
        <el-table-column prop="proportionOfSeriouslyIllEnterprises" label="大病企业比例" width="150px" />
        <el-table-column prop="bigDiseaseEnterprise" label="大病企业" width="150px" />
        <el-table-column prop="personalBaseOfSeriousIllness" label="大病个人基数" width="150px" />
        <el-table-column prop="personalProportionOfSeriousIllness" label="大病个人比例" width="150px" />
        <el-table-column prop="aPersonOfGreatDisease" label="大病个人" width="150px" />
        <el-table-column prop="providentFundNotes" label="公积金备注" width="150px" />
        <el-table-column prop="socialSecurityNotes" label="社保备注" width="150px" />
      </el-table>
    </div>
    <!-- 放置三个按钮 -->
    <el-row type="flex" justify="center" align="middle" style="height:60px">
      <el-button size="small" type="primary" @click="archivingRreportForm">归档{{ yearVal ? yearVal.substr(4):'' }}月份报表</el-button>
      <el-button size="small" type="primary" @click="creatRrportForm">新建报表</el-button>
      <el-button size="small" @click="btnCancel">取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getArchivingCont, getArchivingArchive, newReport } from '@/api/social'
import { formatDate } from '@/filters'
export default {
  data() {
    return {
      yearVal: this.$route.query.yearMonth, // 获取当前月份
      monthStatementData: [],
      loading: false
    }
  },
  created() {
    // 获取公司所有员工社保配置信息
    this.getArchivingCont()
  },
  methods: {
    async getArchivingCont() {
      // console.log(this.yearVal)
      this.loading = true
      this.monthStatementData = await getArchivingCont({ month: this.yearVal, opType: 1 })
      this.loading = false
    },
    // 归档报表
    async archivingRreportForm() {
      //  确认归档
      await this.$confirm('',
        {
          iconClass: 'el-icon-question',
          title: '提示',
          message: `您确定要归档${this.yearVal}月报表?报表归档将覆盖上一次归档记录,无法恢复.`
        }
      )
      //  发送请求
      await getArchivingArchive({ yearMonth: this.yearVal })
      // 提示用户
      this.$message.success('操作成功')
    },
    // 新建报表
    async creatRrportForm() {
      // 获取年数据和月数据
      const nextyearMonth = this.getnextMonth()
      const year = nextyearMonth.substring(0, 4)
      const month = nextyearMonth.substr(4)
      // 确认新建
      await this.$confirm('', {
        iconClass: 'el-icon-info',
        title: '提示',
        message: `您将创建${year}年${month}月报表并归档上一月的报表,请确认`
      })
      // 发请求
      // 1.先归档上一个月的报表
      await getArchivingArchive({ yearMonth: this.yearVal })
      // 2.下一个月份是
      this.yearVal = nextyearMonth
      // 3.新建企业社保配置信息
      // await newReport({ yearMonth: 202004 })
      await newReport({ yearMonth: this.yearVal })
      // 4.查询指定月份的社保信息
      this.getArchivingCont()
      // 提示用户
      this.$message.success('新建成功')
    },
    // 取消
    btnCancel() {
      // 返回上一页
      this.$router.back()
    },
    // 导出excel
    async exportMonthState() {
      // 这里是按钮被点击, 可以就在这里单独引入需要的库
      const { export_json_to_excel } = await import('@/vendor/Export2Excel')
      // console.log(export_json_to_excel)

      // 加载所有员工
      const res = await getArchivingCont({ month: this.yearVal, opType: 1 })
      console.log(res)
      // 准备字典
      const dict = {
        '姓名': 'username',
        '入职时间': 'timeOfEntry',
        '手机号': 'mobile',
        '身份证号码': 'idNumber',
        '学历': 'theHighestDegreeOfEducation',
        '开户行': 'bankCardNumber',
        '一级部门': 'firstLevelDepartment',
        '二级部门': 'twoLevelDepartment',
        '工作城市': 'workingCity',
        '社保电脑号': 'socialSecurityComputerNumber',
        '公积金账号': 'providentFundAccount',
        '离职时间': 'leaveDate',
        '户籍类型': 'householdRegistrationType',
        '参保城市': 'participatingInTheCity',
        '社保月份': 'socialSecurityMonth',
        '社保基数': 'socialSecurityBase',
        '社保合计': 'socialSecurity',
        '社保企业': 'socialSecurityEnterprise',
        '社保个人': 'socialSecurityIndividual',
        '公积金城市': 'providentFundCity',
        '公积金月份': 'providentFundMonth',
        '公积金基数': 'providentFundBase',
        '公积金企业基数': 'accumulationFundEnterpriseBase',
        '公积金企业比例': 'proportionOfProvidentFundEnterprises',
        '公积金个人基数': 'individualBaseOfProvidentFund',
        '公积金个人比例': 'personalRatioOfProvidentFund',
        '公积金合计': 'totalProvidentFund',
        '公积金企业': 'providentFundEnterprises',
        '公积金个人': 'providentFundIndividual',
        '养老企业基数': 'pensionEnterpriseBase',
        '养老企业比例': 'proportionOfPensionEnterprises',
        '养老企业': 'pensionEnterprise',
        '养老个人基数': 'personalPensionBase',
        '养老个人比例': 'personalPensionRatio',
        '养老个人': 'oldAgeIndividual',
        '失业企业基数': 'unemploymentEnterpriseBase',
        '失业企业比例': 'proportionOfUnemployedEnterprises',
        '失业企业': 'unemployedEnterprise',
        '失业个人基数': 'theNumberOfUnemployedIndividuals',
        '失业个人比例': 'percentageOfUnemployedIndividuals',
        '失业个人': 'unemployedIndividual',
        '医疗企业基数': 'medicalEnterpriseBase',
        '医疗企业比例': 'proportionOfMedicalEnterprises',
        '医疗企业': 'medicalEnterprise',
        '医疗个人基数': 'medicalPersonalBase',
        '医疗个人比例': 'medicalPersonalRatio',
        '医疗个人': 'medicalIndividual',
        '工伤企业基数': 'baseOfIndustrialInjuryEnterprises',
        '工伤企业比例': 'proportionOfIndustrialInjuryEnterprises',
        '工伤企业': 'industrialInjuryEnterprise',
        '生育企业基数': 'fertilityEnterpriseBase',
        '生育企业比例': 'proportionOfFertilityEnterprises',
        '生育企业': 'childbearingEnterprise',
        '大病企业基数': 'baseOfSeriousIllness',
        '大病企业比例': 'proportionOfSeriouslyIllEnterprises',
        '大病企业': 'bigDiseaseEnterprise',
        '大病个人基数': 'personalBaseOfSeriousIllness',
        '大病个人比例': 'personalProportionOfSeriousIllness',
        '大病个人': 'aPersonOfGreatDisease',
        '公积金备注': 'providentFundNotes',
        '社保备注': 'socialSecurityNotes'
      }
      console.log(Object.keys(dict))

      const userData = res.map(user => {
        return this.object2Arrayy(user, dict)
      })

      console.log(userData)

      export_json_to_excel({
        header: Object.keys(dict),
        data: userData
      })
    },
    object2Arrayy(user, dict) {
      const newUser = []
      for (const key in dict) {
        const enKey = dict[key]
        const value = user[enKey]
        if (enKey === 'timeOfEntry') {
          newUser.push(new Date(formatDate(value)))
        } else {
          newUser.push(value)
        }
      }
      return newUser
    },
    // 封装获取下一个月份的函数
    getnextMonth() {
      const year = this.yearVal.substring(0, 4)
      const month = this.yearVal.substr(4)
      let nextyear = year
      let nextmonth = parseInt(month) + 1
      // 大于12就加一年
      if (nextmonth > 12) {
        nextyear = parseInt(year) + 1
        nextmonth = 1
      }
      // console.log(nextyear)
      // console.log(nextmonth)
      if (nextmonth < 10) {
        nextmonth = '0' + nextmonth
      }
      // console.log(nextyear)
      // console.log(nextmonth)
      const nextyearMonth = nextyear + nextmonth
      return nextyearMonth
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import './../../styles/variables.scss' ;
  .monthStatementBox{
    padding: 20px;
    .monthStatementTop {
      position: relative;
      background: #fff;
      padding: 10px 15px 0 15px;
      border-bottom: 1px solid #f4f4f4;
      .title{
        color: $blue;
        line-height: 40px;
        border-bottom: 2px solid $blue;
        font-size: 18px;
        font-weight: bold;
        display: inline-block;
        padding: 0 25px;
      }
    }
    .monthStatementTable{
      background: #fff;
      .topLab{
        position: relative;
        padding: 15px;
        div{
          display: inline-block;
          margin: 0 10px;
          span{
            display: inline-block;
            width: 15px;
            height: 15px;
            position: relative;
            top: 2px;
            margin-right: 5px;
          }
        }
        .rightLabBox{
          position: absolute;
          right: -10px;
          top: 10px;
        }
      }
      .itemes:hover{
        background: #fafbff;
      }
      .itemes .lab:hover{
        cursor:pointer;
      }
    }
  }
</style>
